package com.cxm.qyyz.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cxm.qyyz.utils.flow.LogUtils;

import java.lang.ref.WeakReference;

/**
 * author : yzx
 * Time   :2022/2/8
 * desc   :
 */
public class AutoPollRecyclerView extends RecyclerView {

    private final int TIME_AUTO_POLL = 10;
    AutoPollTask autoPollTask;
    private boolean running; //表示是否正在自动轮询
    private boolean canRun;//表示是否可以自动轮询
    private final float scroll_x = 35;//滑动距离
    int position = 0;
    LinearLayoutManager layoutManager;
    boolean stop = false;//是否强制停止
    int s;


    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        autoPollTask = new AutoPollTask(this);
    }

    static class AutoPollTask implements Runnable {
        private final WeakReference<AutoPollRecyclerView> mReference;

        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollTask(AutoPollRecyclerView reference) {
            this.mReference = new WeakReference<AutoPollRecyclerView>(reference);
        }

        @Override
        public void run() {
            AutoPollRecyclerView recyclerView = mReference.get();
            if (recyclerView.stop){
                recyclerView.removeCallbacks(recyclerView.autoPollTask);
            }else {
                if (null == recyclerView.layoutManager) {
                    if (recyclerView.autoPollTask == null )return;
                    if (recyclerView.running && recyclerView.canRun) {
                        recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);
                        recyclerView.scrollBy((int) recyclerView.scroll_x, 0);
                    }

                } else {
                    if (recyclerView.position - POSITION == recyclerView.layoutManager.findFirstVisibleItemPosition()) { // 减速到居中
                        View viewByPosition = recyclerView.layoutManager.findViewByPosition(recyclerView.position - POSITION);
                        int i = viewByPosition.getLeft() + recyclerView.s + recyclerView.s * (POSITION-1)*2 ;
                        recyclerView.smoothScrollBy(i, 0, null, 3500);
                        recyclerView.removeCallbacks(recyclerView.autoPollTask);
                    } else {
                        if (recyclerView.running && recyclerView.canRun) {
                            recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);
                            recyclerView.scrollBy((int) recyclerView.scroll_x, 0);
                        }
                    }
                }
            }
        }
    }
    private  static  int POSITION = 3;

//    private  static  int POSITION = 2;

    //开启:如果正在运行,先停止->再开启
    public void start() {
        stop = false;
        if (running)
            stop(-1, null, 0);
        canRun = true;
        running = true;
        layoutManager = null;
        postDelayed(autoPollTask, TIME_AUTO_POLL);
    }


    public void onDestroy() {
        removeCallbacks(autoPollTask);
        this.layoutManager = null;
        autoPollTask = null;
    }

    public void stop(int position, LinearLayoutManager layoutManager, int w) {
        if (position == -1) {
            return;
        }
        this.position = position;
        this.s = w;
        this.layoutManager = layoutManager;
    }

    public void stop() {
        stop = true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (running)
                    stop(-1, null, 0);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (canRun)
                    start();
                break;
        }
        return super.onTouchEvent(e);
    }
}
